﻿using System;
using System.Configuration;
using Newtonsoft.Json;
using Sustainalytics.ClientDW.DataAccessLayer;
using Sustainalytics.ClientDW.DataAccessLayer.ESGRatings;
using Sustainalytics.DataLayer;
using Sustainalytics.DataLayer.ConnectionStrings;
using Sustainalytics.DataLayer.Shared;
using Sustainalytics.Entities.ConnectionStrings;
using Sustainalytics.Entities.ESGRatings;
using Sustainalytics.EsgRatings.BusinessLogic;
using Sustainalytics.ESGRatings.Tasks.Exceptions;
using Sustainalytics.LongRunningTaskManager;
using Sustainalytics.LongRunningTaskManager.Interface;

namespace Sustainalytics.ESGRatings.Tasks
{
    using BusinessLogic;
    using Sustainalytics.DataLayer.EsgRatings;
    using Sustainalytics.ESGRatings.Tasks.Models;

    public class WeightMatrixDownloadTask : ILongRunningTask
    {
        public LongRunningTaskTypeEnum Type
        {
            get { return LongRunningTaskTypeEnum.WeightMatrixDownload; }
        }

        public object Start(LongRunningTaskContext context)
        {
            var taskParams = GetTaskParams(context);

            var clientConnectionString = ConfigurationManager.ConnectionStrings["ClientPlatform"].ConnectionString;
            var connectionStringsManager =
                ConnectionStringsManager.GetInstance(ConnectionStringsRepository.GetInstance(clientConnectionString));
            var clientDwConnectionString = connectionStringsManager.GetConnectionString(ConnectionStringType.ClientDW);

            var clientDwRepository = ResearchProductsDataReader.GetInstance(clientDwConnectionString);

            var weightMatricesRepository = WeightMatricesRepository.GetInstance(clientDwConnectionString);

            var corporateData = clientDwRepository.ListCompaniesCorporateData().Result;
            var rpIndicators = clientDwRepository.ListIndicatorTemplatesNames().Result;
            
            var weightMatricesManagement = new WeightMatricesProcessor(weightMatricesRepository);

            var weightMatrixReport = weightMatricesManagement.GetWeightMatrixReport(taskParams.WeightMatrixId,rpIndicators, corporateData);

            return new LongRunningTaskFileInMemResult
            {
                Contents = weightMatrixReport.ReportContent,
                FileName = string.Format("{0} {1:ddMMyyyy}.xlsx", weightMatrixReport.WeightMatrixName, DateTime.UtcNow),
                MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            };
        }

        private WeightMatrixDownloadTaskParams GetTaskParams(LongRunningTaskContext context)
        {
            if (string.IsNullOrEmpty(context.ParamJson))
            {
                throw new TaskInvalidParamsException { TaskParams = context.ParamJson };
            }

            // get parameters
            var taskParams = JsonConvert.DeserializeObject<WeightMatrixDownloadTaskParams>(context.ParamJson);

            if (taskParams == null)
            {
                throw new TaskInvalidParamsException { TaskParams = context.ParamJson };
            }

            return taskParams;
        }
    }
}
